package com.haodf.skeleton.anim

import android.animation.ValueAnimator
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.animation.DecelerateInterpolator
import com.haodf.skeleton.R
import com.haodf.skeleton.SkeletonView

/**
 * @author xiaxiao
 * @date 2023/5/10.
 * 一条反光带一闪而过
 */
class BaiJvGuoXiLoadingAnimtor(val skeletonView: SkeletonView) : LoadingAnimator() {

    var animRect: Rect? = null
    var d: Drawable? = null

    override fun drawFrame(canvas: Canvas) {
        canvas.rotate(10f)
        d?.draw(canvas)
    }

    override fun onInit() {
        if (animRect == null) {
            animRect = Rect(0, -200, 200, skeletonView.measuredHeight + 400)
            d = skeletonView.context.resources.getDrawable(R.drawable.shape_anim, null)
        }
    }

    override fun createAnim(): ValueAnimator {
        var prior = 0
        val anim = ValueAnimator.ofInt(-200, skeletonView.measuredWidth + 400)//200
        anim?.interpolator = DecelerateInterpolator()
        anim?.repeatMode = ValueAnimator.RESTART

        anim?.repeatCount = ValueAnimator.INFINITE
        anim?.addUpdateListener {
            if ((it.animatedValue as Int) != prior) {
                animRect?.left = it.animatedValue as Int
                animRect?.right = (animRect?.left ?: 0) + 200
                d?.bounds = animRect ?: return@addUpdateListener
                skeletonView.invalidate()
            }
        }
        anim?.duration = 800
        return anim
    }

}